package com.syni.mdd.yhd.common.all.controller.api.order;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsDxOrderDao;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuy;
import com.syni.mdd.yhd.common.all.entity.BmsDxOrder;
import com.syni.mdd.yhd.common.all.entity.user.BmsUser;
import com.syni.mdd.yhd.common.all.quartz.order.job.OrderCancelTimingJob;
import com.syni.mdd.yhd.common.all.service.*;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.RpcResponseVo;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiOrder.ApiOrderDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiOrder.FindByOrderIdInDto;
import com.syni.mdd.sjb.common.component.utils.feign.dto.mddAll.apiOrder.GetByBusinessOrderForDataBankDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @className OrderRestApi
 * @description TOO
 * @Author cfx
 * @DATE 2020/3/18 10:06
 * @VERSION 1.0
 **/
@Service
public class OrderRestApiService {

    @Autowired
    BmsDxOrderDao bmsDxOrderDao;

    @Autowired
    BmsDxOrderService bmsDxOrderService;
    @Autowired
    BmsUserService bmsUserService;
    @Autowired
    BmsDxGroupBuyService bmsDxGroupBuyService;
    @Autowired
    BmsBusinessService bmsBusinessService;

    @Autowired
    BmsUserBusinessService bmsUserBusinessService;

    @Autowired
    BmsUserBusinessAuthService bmsUserBusinessAuthService;

    public RpcResponseVo timeToCancel(Integer orderId, String cancelTime) {
        BmsDxOrder bmsDxOrder = bmsDxOrderDao.findByIdAndIsDelete(orderId, Constants.COMMON_FLAG_FALSE_INT);
        if(bmsDxOrder == null){
            return RpcResponseVo.result(ResultCode.CODE_ORDER_NO_EXIST, ResultMsg.CODE_ORDER_NO_EXIST);
        }
        Date cancelTimes = DateTimeUtils.converStringToDate(cancelTime);
        bmsDxOrderService.doOrderCancelTiming(OrderCancelTimingJob.class,cancelTimes,bmsDxOrder);
        return RpcResponseVo.success(null);
    }



    public RpcResponseVo findByOrderIdIn(FindByOrderIdInDto findByOrderIdInDto, Pageable pageable) {
        Map<String,Object> conditionMap = new HashMap<>();
        Set<Integer> orderIds = findByOrderIdInDto.getOrderIds();
        if(orderIds.size()>0){
            conditionMap.put("id_IN",findByOrderIdInDto.getOrderIds());
        }else{
            orderIds.add(0);
            conditionMap.put("id_IN",orderIds);
        }
        String searchStr = findByOrderIdInDto.getSearchStr();
        if(searchStr != null && !searchStr.isEmpty()){
            Set<Integer> businessByVendorName = bmsBusinessService.findBusinessByVendorName(searchStr);
//            conditionMap.put("vendorName_LIKE",findByOrderIdInDto.getSearchStr());
            if(businessByVendorName.size()==0){
                businessByVendorName.add(0);
            }
            conditionMap.put("bmsBusinessId_IN",businessByVendorName);
        }

        List all = bmsDxOrderDao.findAll(new SpecificationUtils(conditionMap));
        Page<BmsDxOrder> bmsOrderPage = bmsDxOrderDao.findAll(new SpecificationUtils(conditionMap),pageable);
        List<ApiOrderDto> apiOrderDtoList = BeanUtils.copyList(bmsOrderPage.getContent(),ApiOrderDto.class);
        Set<Integer> userIds = new HashSet<>();
        Set<Integer> groupIds = new HashSet<>();
        Set<Integer> businessIds = new HashSet<>();
        for(ApiOrderDto a : apiOrderDtoList){
            userIds.add(a.getBmsUserId());
            groupIds.add(a.getBmsGroupBuyId());
            businessIds.add(a.getBmsBusinessId());
        }
        Map<Integer, BmsUser> userByIds = bmsUserService.getUserByIds(userIds);
        Map<Integer, BmsDxGroupBuy> groupBuyById = bmsDxGroupBuyService.getGroupBuyById(groupIds);
        Map<Integer, BmsBusiness> businessByIds = bmsBusinessService.findBusinessByIds(businessIds);
        for(ApiOrderDto a : apiOrderDtoList){
            if (userByIds.containsKey(a.getBmsUserId())) {
                a.setLoginName(userByIds.get(a.getBmsUserId()).getLoginName());
                a.setHeadImg(userByIds.get(a.getBmsUserId()).getHeadImg());
            }
            if (groupBuyById.containsKey(a.getBmsGroupBuyId())) {
                a.setGroupName(groupBuyById.get(a.getBmsGroupBuyId()).getGroupName());
                a.setExpireTime(groupBuyById.get(a.getBmsGroupBuyId()).getExpireTime());
                a.setGroupImg(groupBuyById.get(a.getBmsGroupBuyId()).getGroupImg());
            }
            if (businessByIds.containsKey(a.getBmsBusinessId())) {
                a.setVendorName(businessByIds.get(a.getBmsBusinessId()).getVendorName());
                a.setLogoUrl(businessByIds.get(a.getBmsBusinessId()).getLogoUrl());
            }
        }
        findByOrderIdInDto.setCount(all.size());
        findByOrderIdInDto.setApiOrderDtos(apiOrderDtoList);
        return RpcResponseVo.success(findByOrderIdInDto,ResultMsg.HAND_SUCCESS);
    }

    public Map<String,Object> getByBusinessOrderForDataBank(GetByBusinessOrderForDataBankDto getByBusinessOrderForDataBankDto) {
        return bmsUserBusinessService.getOrderDataForDataBank(getByBusinessOrderForDataBankDto);
    }



}
